<template>
  <section>
    <div v-show="showBody">
      <XdoCard :bordered="false">
        <div ref="area_head">
          <XdoBreadCrumb show-icon>
            <XdoButton type="primary" class="dc-margin-right" :loading="searchLoading" @click="handleSearchSubmit">查询</XdoButton>
            <XdoButton type="warning" class="dc-margin-right" @click="handleShowSearch">查询条件</XdoButton>
          </XdoBreadCrumb>
        </div>
        <div ref="area_search" >
          <div v-show="showSearch">
            <div class="separateLine"></div>
            <DcBillListXwtListSearch ref="listSearch"></DcBillListXwtListSearch>
          </div>
        </div>
      </XdoCard>
      <XdoCard :bordered="false">
        <div class="action" ref="area_actions">
          <template v-for="item in actions">
            <XdoButton v-if="item.needed === true" :type="item.type" :disabled="item.disabled" :loading="item.loading" style="font-size: 12px" :class="item.key"
                    @click="item.click" :key="item.label">
              <Icon :type="item.icon" size="22" class="xdo-icon"/>{{ item.label }}</XdoButton>&nbsp;
          </template>
          <ExportAsync :param="exportConfig" :click="handleAsyncExport" :columns="columnsInfo" />
        </div>
        <div style="text-align: right">
          <strong>数量汇总(仅数值) 申报数量:{{qty}}  法定数量:{{qty1}}  法定第二数量:{{qty2}}  申报总价:{{decTotal}}  净重:{{netWt}}  毛重:{{grossWt}}</strong>
        </div>
      </XdoCard>
      <XdoCard :bordered="false">
        <!--@on-row-dblclick="handleRowDblClick"-->
        <XdoTable class="dc-table" ref="table" :columns="gridConfig.gridColumns" :height="dynamicHeight"
                 :data="gridConfig.data" :loading="tableLoading" stripe :border="gridConfig.data.length > 0" @on-selection-change="handleSelectionChange"></XdoTable>
        <div ref="area_page">
          <XdoPage class="dc-page" :current="pageParam.page" :page-size='pageParam.limit' :total="pageParam.dataTotal" show-total show-sizer
                   @on-change="pageChange" @on-page-size-change="pageSizeChange" :page-size-opts="[15,20,30,40]"/>
        </div>
      </XdoCard>
    </div>
    <DcBillListXwtEdit v-if="!showBody" @onEditBack="editBack" :editConfig="editConfig"></DcBillListXwtEdit>
    <ImportPage :importShow.sync="importShow" :importConfig="importConfig"></ImportPage>
  </section>
</template>
<script>
  import DcBillListXwtListSearch from './DcBillListXwtListSearch'
  import DcBillListXwtEdit from './DcBillListXwtEdit'
  import { jx_bill_testUri,excelExport } from '@/api'
  import { getColumnsByConfig ,getExcelColumnsByConfig } from '@/common'
  import { columnsConfig,columns ,excelColumnsConfig } from './dcBillListXwtColumns'
  import { commonProcess } from '@/view/jx_bill_test-common'
  import { editStatus } from '@/view/jx_bill_test-common/constant'
  import ImportPage from 'xdo-import'
  import { xwManageListUrl } from '../../../api/jx_bill_test_api_constant'
  export default {
    name: 'DcBillListXwtList',
    components: {
      DcBillListXwtListSearch,
      DcBillListXwtEdit,
      ImportPage
    },
    mixins: [ columns, commonProcess ],
    props: {
      headId: {type: String, default: ''},
      emsNo:{type: String,default: ''},
      gMark:{type: String,default: ''},
      status:{type: String,default: ''},
      rotateApplyNo:{type: String,default: ''},
      operationStatus: { type: String, default: editStatus.SHOW },
      declareType:{type: String,default: ''},
      tradeMode:{type: String,default: ''},
      billListNo:{type: String,default: ''}
    },
    data() {
      return {
        qty:'0',
        qty1:'0',
        qty2:'0',
        decTotal:'0',
        netWt:'0',
        grossWt:'0',
        showBody: true,
        hasChildTabs: true,
        actions: [
          { type: 'text', disabled: false, click: this.handleAdd, icon: 'ios-add', label: '新增', key:'xdo-btn-add', loading: false, needed: true },
          { type: 'text', disabled: false, click: this.handleDelete, icon: 'ios-trash-outline', label: '删除', key:'xdo-btn-delete', loading: false, needed: true },
          { type: 'text', disabled: false, click: this.handleImport, icon: 'ios-cloud-upload-outline', label: '导入', key:'xdo-btn-upload', loading: false, needed: true },
          { type: 'text', disabled: false, click: this.handleExport, icon: 'ios-cloud-download-outline', label: '同步导出', key:'xdo-btn-export', loading: false, needed: true },
        ],
        /* --------- 导入开始 ---------*/
        importShow:false, // 是否显示
        importConfig:{
          taskCode:'XXX', // 任务编码
          businessParam: {    // 自定义业务参数
            insertUser: this.$store.state.user.userNo,
            accessToken: this.$store.state.token,
            // 其余业务参数
          },
          systemCode: 'XXX',  // 系统标识
          tradeCode: this.$store.state.user.company,  // 企业编码
          httpApi: this.$http  // http_api接口对象
        },
        /* --------- 导入结束 ---------*/
        /* --------- 异步导出开始 ---------*/
        exportConfig:{
          taskCode: "xxx",  // 添加任务使用的taskCode
          systemCode: "xxx", // 添加任务使用的systemCode
          displayTaskCode: "", // 下载中心所使用的过滤条件，不填默认使用taskCode
          displaySystemCode: "", // 下载中心所使用的过滤条件，不填默认使用systemCode
          fileName: "",
          taskName: "",
          exportParamJson: "",
          enableSharding: true
          },
          columnsInfo:[],
        /* --------- 异步导出结束 ---------*/
      }
    },
    mounted() {

      this.editConfig.headId = this.headId
      this.editConfig.status=this.status
      this.editConfig.emsNo=this.emsNo
      this.editConfig.gMark=this.gMark
      this.editConfig.declareType=this.declareType
      this.editConfig.rotateApplyNo=this.rotateApplyNo
      this.editConfig.tradeMode=this.tradeMode
      this.editConfig.billListNo=this.billListNo
      this.gridConfig.gridColumns = getColumnsByConfig(this.totalColumns, columnsConfig)
      this.refreshDynamicHeight()
      this.getList()
      this.getDataSummary();
      if (this.operationStatus === editStatus.SHOW) {
        this.actions = this.actions.filter(item => item.key === 'xdo-btn-download')
      }
      // 设置异步导出列
      this.columnsInfo = this.getAsyncExportColumns(this.gridConfig.gridColumns)
    },
    methods: {
      //显示数据汇总信息
      getDataSummary(){
        this.$http.post(jx_bill_testUri.xwManageListUrl.dcBillListXwt.getDataSummaryByHeadId,{sid:this.editConfig.headId}).then(res => {
          if (res.data.success){
              this.qty=res.data.data.qty;
              this.qty1=res.data.data.qty1;
              this.qty2=res.data.data.qty2;
              this.decTotal=res.data.data.decTotal;
              this.netWt=res.data.data.netWt;
              this.grossWt=res.data.data.grossWt;
              if (res.data.data.qty==null||res.data.data.qty==''){
                this.qty='0';
              }
              if (res.data.data.qty1==null){
                this.qty1='0';
              }
              if (res.data.data.qty2==null){
                this.qty2='0';
              }
              if (res.data.data.decTotal==null){
                this.decTotal='0';
              }
             if (res.data.data.netWt==null){
                this.netWt='0';
              }
             if (res.data.data.grossWt==null){
                this.grossWt='0';
              }
          }
        }, () => {
        })
      },
      getList() {
        this.tableLoading = true
        this.$nextTick(() => {
          let params = Object.assign({}, this.getSearchParams())
          // 其他参数赋值
          params.headId= this.headId
          //params.iEMark = "E"
          //params.emsNo = "E2325B0A0008"
          this.$http.post(jx_bill_testUri.xwManageListUrl.dcBillListXwt.list, params, {params: this.pageParam}).then(res => {
            if (res.data.success) {
              this.tableLoading = false
              this.gridConfig.data = res.data.data
              this.pageParam.dataTotal = res.data.total
            }
          }, () => {})
        })
      },
      handleEdit() {

          this.editConfig.editStatus = editStatus.EDIT
          this.editConfig.headId = this.gridConfig.selectData.headId
          this.editConfig.editData = this.gridConfig.selectData
          this.showBody = false
      },
      handleView() {
          this.editConfig.editStatus = editStatus.SHOW
          this.editConfig.headId = this.gridConfig.selectData.sid
          this.editConfig.editData = this.gridConfig.selectData
          this.showBody = false
      },
      handleAdd() {
          this.gridConfig.selectRows = []
          this.editConfig.editStatus = editStatus.ADD
          this.editConfig.editData = {}
          this.showBody = false
      },
      handleDelete() {
        this.status=this.editConfig.status;
        //检验当前状态下保存按钮是否能用
        if (this.status=='B1' || this.status=='C1' || this.status=='0' ||
          this.status=='1' || this.status=='2' || this.status=='DL' ||
          this.status=='4' || this.status=='5' || this.status=='Y'){
          alert("当前状态不可删除!")
          return;
        }
        if (((this.status=='D1' && this.editConfig.declareType!='1')||
          (this.status=='3' && this.editConfig.declareType!='1')||
          (this.status=='Z' && this.editConfig.declareType!='1'))){
          alert("当前状态不可删除!")
          return;
        }
        if (
          (this.editConfig.status!='A0'&&this.editConfig.status!='3'&&this.editConfig.status!='Z'&&this.editConfig.status!='D1'&&this.editConfig.declareType!='1')
          ||
          (this.editConfig.status!='A0'&&this.editConfig.status!='3'&&this.editConfig.status!='Z'&&this.editConfig.status!='D1'&&this.editConfig.declareType=='1')
        ){
          alert("有不符合条件的数据存在，无法删除！");
          return;
        }
        this.doDelete(jx_bill_testUri.xwManageListUrl.dcBillListXwt.delete)
      },
      editBack(val) {
        if(val){
          this.showBody = true
          this.gridConfig.selectRows = []
          this.getList()
        }
      },
      // 导入
      handleImport() {
        this.importShow = true
      },
      // 同步导出
      handleExport(){
        let columns = getExcelColumnsByConfig(this.totalColumns,excelColumnsConfig)
        let url = jx_bill_testUri.xwManageListUrl.dcBillListXwt.export
        let exportName = 'xxx导出' // 根据需要，设置导出文件名称，待实现
        let params = {
          "header": columns,
          "name": exportName,
          "queryParam": this.getSearchParams()
        }
        excelExport(url,params)
      },
      // 异步导出
      handleAsyncExport(){
        this.exportConfig.taskName = "xxx导出"
        this.exportConfig.fileName = this.$store.state.user.userNo + this.exportConfig.taskName + "_" + this.getCurrentDateString() + ".xlsx"
        let queryParam = this.getSearchParams()
        delete queryParam.limit
        delete queryParam.page
        let params = {
          "queryParam": queryParam
        }
        this.exportConfig.exportParamJson = params.queryParam
      }
    }
  }
</script>
<style lang="less" scoped>
</style>
